<?php

/**
 * User: LuYuanXun
 * Date: 2018/09/11
 * Time: 15:15
 * Description:HB电子的注单拉取
 */

namespace App\Console\Commands\ThirdOrder;


use App\Services\ThirdAPI\GameApiService;
use App\Services\ThirdAPI\MWAPIService;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;
use App\Services\UserService;


class MWOrderProcessor extends BaseOrderProcessor
{
    protected $signature = self::PREFIX . 'mw';
    protected $description = 'MW注单拉取';

    /**
     * @var MWAPIService
     */
    protected $mwService;
    protected $reportOrderType;
    protected $gameNames;
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = 60 * 60;
    // 自动重拉更新锁 redis key
    public $lockRedisKey = RedisKeys::MW_LOCK_AUTOFIX;
    // 拉取注单更新时间 redis key
    public $updateRedisKey = RedisKeys::MW_UPDATE_TIME;
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;

    /**
     * 初始化信息，子类需要重写这个方法
     * @param null $argv
     * @throws \Exception
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        $this->mwService = new MWAPIService();
        // 5.第三方 partner id，名字，对应第三方表id
        $this->partnerName = "MW";
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_EGAME;
        $this->reportOrderType = self::GAME_TYPE_EGAME;
        $this->gameNames = $this->mwService->getGameNames();
        $this->userInfos = [];
    }

    /**
     * 向第三方API抓取数据
     * @return mixed
     * @throws \Exception
     */
    public function catchData()
    {
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey);
        $startTime = strtotime($startTime);
        // 拉取 $this->timeInterval 的数据，减5秒是防止出现临界数据
        $this->latestUpdateKey = $endTime = min(time(), $startTime + $this->timeInterval) - 5;
        return $this->mwService->getOrders($startTime, $endTime);
    }

    /**
     *  处理数据
     * 不可以放复杂操作，因为拉取到的每个数据都会经过 load 加载
     * @param $data
     */
    public function load($data)
    {
        $logDate = $this->mwService->handleTime(strtotime($data['logDate']));
        $this->apiUserName = $data['uid'];
        $this->orderNumber = strval($data["gameNum"]);
        $this->betMoney = $this->validMoney = amount_format($data['playMoney'], true); //传过来的是元，要转成厘 2017-07-05
        $this->prizeMoney = empty($data['winMoney']) ? 0 : amount_format($data['winMoney'], true);
        $this->winLossMoney = $this->prizeMoney - $this->betMoney;
        $this->commonStatus = self::COMMON_STATUS_SETTLE;
        $this->apiStatus = strval(self::COMMON_STATUS_SETTLE);
        $this->result = '';
        $this->gameCode = $data['gameId'];
        $this->gameName = $data['gameName'];
        $this->betContent = "";
        $this->updateKey = $data['logDate'];//保持官方返回的北京时间
        $this->betTime = $logDate;
        $this->reportTime = $data['logDate'];
        $this->originData = json_encode($data);
        $this->settleTime = $logDate;
        // 没有贡献和彩金
        $this->contributeMoney = 0;
        $this->bonusMoney = 0;
    }

    /*
     * 更新redis拉取注单ID
     */
    public function updateFlag($updateKey)
    {
        Redis::connection()->set($this->updateRedisKey, date("Y-m-d H:i:s", $this->latestUpdateKey));
    }

    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    public function initFromData($data)
    {

    }

    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    public function fixData($date)
    {

    }
}